<template>
  <div class="app-container">
    <!-- <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane label="语音讲解" name="1"></el-tab-pane>
       <el-tab-pane label="一级导游讲解" name="2"></el-tab-pane> -->
    <!-- </el-tabs> --> 
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属景区" prop="scenicId">
        <el-select
          v-model="queryParams.scenicId"
          placeholder="请选择"
          filterable
          clearable
        >
          <el-option
            v-for="item in regList"
            :key="item.id"
            :label="item.scenicName"
            :value="item.id"
            style="width: 350px"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-plus" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-button icon="el-icon-plus" @click="handleAdd">新增</el-button> -->
    <el-table :data="routeList" stripe style="width: 100%">
      <el-table-column prop="title" label="名称"> </el-table-column>
      <el-table-column prop="name" label="所属景区" >
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column prop="updateTime" label="修改时间"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="editRow(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.prevent="deleteRow(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col >
            <el-form-item label="所属景区" prop="scenicId" >
              <el-select v-model="form.scenicId" placeholder="请选择" @change="onChange" filterable clearable >
                <el-option
                  v-for="item in regList"
                  :key="item.id"
                  :label="item.scenicName"
                  :value="item.id"
                  style="width: 350px"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路线名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入路线名称" />
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item
              v-for="(domain, index) in form.domains"
              :label="'路线' + index"
              :key="index"
              :prop="'domains.' + index + '.value'"
              :rules="{
                required: true,
                message: '景点不能为空',
                trigger: 'blur',
              }"
            >
              <el-select v-model="domain.value" placeholder="请选择">
                <el-option
                  v-for="item in allAreaList"
                  :key="item.id"
                  :label="item.scenicName"
                  :value="item.id"
                  style="width: 290px"
                >
                </el-option>
              </el-select>
              <el-button @click.prevent="removeDomain(domain)">删除</el-button>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item>
              <el-button @click="addDomain">新增景点</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRoute,
  addRoute,
  editRoute,
  deleteRoute,
  getRoute,
  getAllAreaList,
  getAllAreaSpotList,//获取景点
} from "@/api/system/route";

import {getAllRegList} from "@/api/system/scenic";//获取景区

export default {
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      loading: true,
      total: 0,
      routeList: [],
      activeName: "1",
      queryParams: {
        current: 1,
        size: 10,
      },
      // 表单参数
      form: {
        domains: [
          {
            value: "",
          },
        ],
        title: "",
        scenicId:null,
      },
      allAreaList: [],
      regList:[],
      title: "",
      open: false,
      // 表单校验
      rules: {
        title: [
          { required: true, message: "路线名称不能为空", trigger: "blur" },
        ],
        scenicId: [
          { required: true, message: "所属景区不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getAllList();
  },
  methods: {
    getList() {
      this.loading = true;
      listRoute({...this.queryParams,language: Number(this.activeName)}).then((response) => {
        this.routeList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    handleTabClick(tab, event) {
      this.getList();
      
    },
    getAllAreaList() {
      getAllAreaList({language: Number(this.activeName)}).then((response) => {
        this.allAreaList = response.data;
      });
    },
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        scenicId: undefined,
        domains: [],
      };
      this.resetForm("form");
    },
    deleteRow(row) {
      const id = row.id;
      this.$modal
        .confirm("是否确认删除该路线？")
        .then(function () {
          return deleteRoute({ id: id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    getAllList() {
      getAllRegList().then(response => {
        this.regList = response.data;
      });
    },
    onChange(event) {
      this.form.domains = [];
      if(event){
        getAllAreaSpotList({language: Number(this.activeName),scenicId:event}).then((response) => {
        this.allAreaList = response.data; 
      }); 
      }else{
        // 清空select数据
        this.allAreaList = []; 
      }
      
    },
    editRow(row) {
      this.reset();
      const id = row.id;
      // this.getAllAreaList()
      getRoute({ id: id }).then((response) => {
        getAllAreaSpotList({language: Number(this.activeName),scenicId: row.scenicId}).then((res) => {
          this.allAreaList = res.data;
          let data = response.data;
          this.form.id = data.id;
          this.form.title = data.title;
          this.form.scenicId = data.scenicId;
          let list = [];
          data.site.split(",").map((item) => {
            list.push({ value: Number(item) });
          });
          this.form.domains = list;
          this.open = true;
          this.title = "修改路线";
        });
      });
    },
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    handleAdd() {
      getAllAreaList({language: Number(this.activeName)}).then((response) => {
        this.allAreaList = response.data;
        this.reset();
        this.open = true;
        this.title = "添加路线";
      });
    },
    handleQuery() {
      // this.queryParams.current = 1;
      this.getList();
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        console.log("上传数据", this.form);
        if (valid) {
          let title = this.form.title;
          let scenicId = this.form.scenicId;
          let list = this.form.domains;
          let site = [];
          (list || []).map((item) => {
            site.push(item.value);
          });
          if (this.form.id != undefined) {
            editRoute({scenicId, title, site: site.join(","), id: this.form.id ,language: Number(this.activeName),}).then(
              (response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            );
          } else {
            addRoute({scenicId, title, site: site.join(",") ,language: Number(this.activeName),}).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    removeDomain(item) {
      var index = this.form.domains.indexOf(item);
      if (index !== -1) {
        this.form.domains.splice(index, 1);
      }
    },
    addDomain() {
      this.form.domains.push({
        value: "",
      });
    },
  },
};
</script>

<style></style>
